# -*- coding:UTF-8 -*-
import json
import urllib, urllib2, sys
import ssl

reload(sys)
sys.setdefaultencoding("utf8")


class Unit:
    __access_token = None

    def __init__(self, UserName):
        self.session_id = ""
        self.UserName = UserName
        self.result = None
        self.action_list = None
        self.say = None
        self.answer = None
        self.intent=None
        self.slot=None

<<<<<<< HEAD
class Unit:   
    __access_token=None
    def __init__(self,UserName):
        self.session_id=""
        self.UserName=UserName
        self.result=None
        self.action_list=None
        self.say=None
        self.answer=None


=======
>>>>>>> 1aa2102583626cc1d3c80f4fbcaa8738646c845d
    @staticmethod
    def Token():
        if Unit.__access_token == None:
            return True
        else:
            return False

    @staticmethod
    def UpdateAccess_token():
        print("update")
        host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=QGluDErR2pcurfpapWa19GXQ&client_secret=leH2yN1F4Blr6MLC7uQ58AMHcHcYeIG7'
        request = urllib2.Request(host)
        request.add_header('Content-Type', 'application/json; charset=UTF-8')
        response = urllib2.urlopen(request)
        content = response.read()
        if (content):
            print("access_token加载成功")
            getJson = json.loads(content)
            Unit.__access_token = getJson['access_token']
            return
        else:
<<<<<<< HEAD
            return 
    def Post(self,str):
        if(Unit.__access_token!=None): 
            print( "session:")  
            print(self.session_id)
=======
            return

    def Post(self, str):
        if (Unit.__access_token != None):
            print("session:")
            print self.session_id
>>>>>>> 1aa2102583626cc1d3c80f4fbcaa8738646c845d
            url = 'https://aip.baidubce.com/rpc/2.0/solution/v1/unit_utterance?access_token=' + Unit.__access_token
            # post_data = "{\"scene_id\":3871,\"query\":"+str+",+\"session_id\":\" \"}"
            post_data = "{\"scene_id\":3871,\"query\":\"" + str + "\",\"session_id\":\"" + self.session_id + "\"}"
            # print 1
            request = urllib2.Request(url, post_data)
            request.add_header('Content-Type', 'application/json')
            response = urllib2.urlopen(request)
            content = response.read()
            if (content):
                self.answer = json.loads(content)
                # print(answer)
                # print(answer['result']['action_list'][0])
                action_list_d = json.dumps(self.answer['result']['action_list'][0])
                self.result = self.answer['result']
                self.intent = json.loads(json.dumps(self.result['qu_res']['intent_candidates'][0]))
                print(self.intent)
                # print(self.result['session_id'])
                self.action_list = json.loads(action_list_d)
                if (True):
                    self.session_id = self.result['session_id'].encode("utf-8")
                    # print self.result['session_id']

    def Show(self):
        # if(self.session_id):
        #     print(self.session_id+"\n")
        # else:
        #     print("None session")
        if (self.result):
            print(self.result['qu_res']['intent_candidates'])
            print (self.intent['slots'])
        else:
            print("None result")
<<<<<<< HEAD
        # if(self.action_list):
        #     print(self.action_list)
=======
            # if(self.action_list):
            #     print(self.action_list)

>>>>>>> 1aa2102583626cc1d3c80f4fbcaa8738646c845d
    def getSay(self):

        return self.action_list['say'].encode("utf-8")


Unit.UpdateAccess_token()
z = "我想看电影"
print(z)
<<<<<<< HEAD
my=Unit("Test");
while(z!=""):
    my.Post(z)
    my.Show()
    z=raw_input("-->")
    
=======
my = Unit("Test");
while(z!=""):
    my.Post(z)
    my.Show()
    print my.getSay();
    z=raw_input("-->")
>>>>>>> 1aa2102583626cc1d3c80f4fbcaa8738646c845d
